<?php

class DefaultController extends Controller{
    /**
	 * @return array action filters
	 */
	public function filters(){
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}
    
    /**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules(){
		return array(
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('logout','index'),
				'roles'=>array('admin','normal','news'),
			),
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('login'),
				'users'=>array('*'),
			),
            array('allow',
                'actions'=>array('captcha'),
                'users'=>array('*'),
                ),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
            
		);
	}
    
    public function actions(){
        return array( 
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha'=>array(
                    'class'=>'CCaptchaAction',
                    'backColor'=>0xFFFFFF, 
                    'maxLength'=>'8',       // 最多生成几个字符
                    'minLength'=>'7',       // 最少生成几个字符
                    'height'=>'40',
                    'width'=>'230',
            ), 
        ); 
        
    }

    
	public function actionIndex(){
		if(Yii::app()->request->isPostRequest){
				
		}


		$title = "雅黑PHP探针";
		$version = "v0.4.2"; //版本号
		$timeStart = ProbeWidget::microtimeFloat();	
		$HTTP_HOST = preg_replace('~^www\.~i', '', $_SERVER['HTTP_HOST']);

		$act = isset($_GET['act']) ? $_GET['act'] : '';
		$postAct = isset($_POST['act']) ? $_POST['act'] : '';
		$postFunName = isset($_POST['funName']) ? $_POST['funName'] : '';

		if ($act == "phpinfo"){
			phpinfo();
			exit();
		}elseif($act == "Function"){
			$arr = get_defined_functions();
			Function php()
			{
			}
            header('Content-Type:text/html;charset=utf-8');
			echo "<pre>";
			Echo "这里显示系统所支持的所有函数,和自定义函数\n";
			print_r($arr);
			echo "</pre>";
			exit();
		}elseif($act == "disable_functions"){
			$disFuns=get_cfg_var("disable_functions");
			if(empty($disFuns)){
				$arr = '<font color=red>×</font>';
			}else{ 
				$arr = $disFuns;
			}
			Function php()
			{
			}
			echo "<pre>";
			Echo "这里显示系统被禁用的函数\n";
			print_r($arr);
			echo "</pre>";
			exit();
		}

		//MySQL检测
		if ($postAct == 'MySQL检测'){
			$host = isset($_POST['host']) ? trim($_POST['host']) : '';
			$port = isset($_POST['port']) ? (int) $_POST['port'] : '';
			$login = isset($_POST['login']) ? trim($_POST['login']) : '';
			$password = isset($_POST['password']) ? trim($_POST['password']) : '';
			$host = preg_match('~[^a-z0-9\-\.]+~i', $host) ? '' : $host;
			$port = intval($port) ? intval($port) : '';
			$login = preg_match('~[^a-z0-9\_\-]+~i', $login) ? '' : htmlspecialchars($login);
			$password = is_string($password) ? htmlspecialchars($password) : '';
		}elseif ($postAct == '函数检测'){
			$funRe = "函数".$postFunName."支持状况检测结果：".ProbeWidget::isfun1($postFunName);
		}elseif ($postAct == '邮件检测'){
			$mailRe = "邮件发送检测结果：发送";
			if($_SERVER['SERVER_PORT']==80){
				$mailContent = "http://".$_SERVER['SERVER_NAME'].($_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']);
			}else{
				$mailContent = "http://".$_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT'].($_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']);
			}
			$mailRe .= (false !== @mail($_POST["mailAdd"], $mailContent, "This is a test mail!\n\nhttp://lnmp.org")) ? "完成":"失败";
		}	

	
		// 根据不同系统取得CPU相关信息
		switch(PHP_OS){
			case "Darwin":
				break;
			case "Linux":
				$sysInfo = ProbeWidget::sysLinux();
				$sysReShow = (false !== $sysInfo) ? "show" : "none";
			break;
			
			case "FreeBSD":
				$sysInfo = ProbeWidget::sysFreeBSD();
				$sysReShow = (false !== $sysInfo) ? "show" : "none";
			break;	
			case "WINNT":
				$sysReShow = (false !== ($sysInfo = ProbeWidget::sysWindows()))?"show":"none";
			break;
		
			
			default:
			break;
		}

		switch(PHP_OS){
			case 'Linux':
				$this->render('linux',array(
					'timeStart' => $timeStart,
					'title'=>$title,
					'version'=>$version,
					'httpHost'=>$HTTP_HOST,
					'sysInfo'=>$sysInfo,
					'postAct'=>$postAct,
					'act'=>$act,
					'postFunName'=>$postFunName
				));

				break;
			case 'Darwin':
				$this->render('mac');
				break;
				
		}

	}
    
    public function actionLogin(){
        $model=new AdminLoginForm;
        
        if(isset($_POST['ajax']) && $_POST['ajax']==='login-form'){
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
        
        if(isset($_POST['AdminLoginForm'])){
			$model->attributes=$_POST['AdminLoginForm'];
            
            
			// validate user input and redirect to the previous page if valid
            if($model->validate() && 
                    $model->validateVerifyCode($this->createAction('captcha')->getVerifyCode()) && 
                    $model->adminLogin()){
                
                $this->redirect(CController::createUrl('blog/index'));
            }
				
		}
        $this->render('login',array('model'=>$model));
    }
    
    /**
     * 退出登录
     */
    public function actionLogout(){
		Yii::app()->admin->logout();
		$this->redirect(CController::createUrl('default/login'));
	}
}
